package app.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 搜索关键词排行榜结果实体类 - 符合实时数仓设计规范
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SearchRankResult extends BaseDwsBean {
    
    // 业务主键
    private String keyword;
    
    // 维度字段
    private String categoryId;
    private String categoryName;
    private String keywordType;  // core、modifier、trend
    private String trendType;    // hot、growth、blue_sea
    
    // 搜索行为度量字段
    private Long searchCount;
    private Long clickCount;
    private Long orderCount;
    private Long conversionCount;
    private Double clickRate;
    private Double conversionRate;
    
    // 竞争分析度量字段
    private Long supplyCount;        // 供给商品数量
    private Double demandSupplyRatio; // 需求供给比
    private Double coreStrength;     // 核心词强度
    private Double modifierEffect;   // 修饰词效果
    
    // 增长分析字段
    private Long currentSearchCount;
    private Long previousSearchCount;
    private Double growthRate;
    
    /**
     * 计算点击率
     */
    public void calculateClickRate() {
        if (searchCount != null && searchCount > 0 && clickCount != null) {
            this.clickRate = clickCount.doubleValue() / searchCount.doubleValue() * 100;
        } else {
            this.clickRate = 0.0;
        }
    }
    
    /**
     * 计算转化率
     */
    public void calculateConversionRate() {
        if (clickCount != null && clickCount > 0 && conversionCount != null) {
            this.conversionRate = conversionCount.doubleValue() / clickCount.doubleValue() * 100;
        } else {
            this.conversionRate = 0.0;
        }
    }
    
    /**
     * 计算需求供给比
     */
    public void calculateDemandSupplyRatio() {
        if (supplyCount != null && supplyCount > 0 && searchCount != null) {
            this.demandSupplyRatio = searchCount.doubleValue() / supplyCount.doubleValue();
        } else {
            this.demandSupplyRatio = 0.0;
        }
    }
    
    /**
     * 计算增长率
     */
    public void calculateGrowthRate() {
        if (previousSearchCount != null && previousSearchCount > 0 && currentSearchCount != null) {
            this.growthRate = (currentSearchCount.doubleValue() - previousSearchCount.doubleValue()) 
                             / previousSearchCount.doubleValue() * 100;
        } else {
            this.growthRate = 0.0;
        }
    }
    
    /**
     * 设置搜索数据并计算相关指标
     */
    public void setSearchData(Long current, Long previous) {
        this.currentSearchCount = current;
        this.previousSearchCount = previous;
        this.searchCount = current;
        calculateGrowthRate();
    }
}